package com.luckynote.common.base.util;

import org.apache.dubbo.common.utils.StringUtils;

import java.util.Collection;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * <pre>
 * *********************************************
 * Copyright LUCKY-NOTE.
 * All rights reserved.
 * ****************************************************
 *  Version       Date         Author            Desc
 *    v1.0       2019/7/2      bingo        Mybatis OGNL 工具类
 * ****************************************************
 * </pre>
 */
public class OgnlUtil {

    public static boolean isNotEmpty(Object o){
        return !isEmpty(o);
    }

    /**
     * 可以用于判断String,Map,Collection,Array是否为空
     */
    private static boolean isEmpty(Object o) throws IllegalArgumentException {
        if (o == null) return true;
        if (o instanceof String)
            return StringUtil.isEmptyString((String) o);
        else if (o instanceof Collection)
            return ((Collection) o).isEmpty();
        else if (o instanceof Map)
            return (((Map) o).isEmpty());
        return false;
    }

    /**
     * 校验是否是数字
     * @param o 入参
     * @return  是 or 否
     */
    public static boolean isNumber(Object o) {
        if (o == null) return false;
        if (o instanceof Number) {
            return true;
        }
        if (o instanceof String) {
            String str = (String) o;
            if (StringUtils.isBlank(str)) {
                return false;
            }
            return isNumeric(str);
        }
        return false;
    }

    private static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }
}
